﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace _61文件流
{
    class Program
    {
        static void Main(string[] args)
        {
            // FileStream   操控字节
            //1.创造创建FileStream对象

            //使用Filestream来读取数据
            //FileStream sfRead = new FileStream(@"C:\Users\Administrator\Desktop\1.txt",FileMode.OpenOrCreate, FileAccess.Read);
            //byte[] buffer = new byte[1024 * 1024 * 5];
            ////返回本次实际读取到的有效字节数
            //int r = sfRead.Read(buffer, 0, buffer.Length);
            ////将字节数组中每一个元素按照指定的编码格式解码成字符串
            //string s = Encoding.UTF8.GetString(buffer,0,r);
            ////关闭流
            //sfRead.Close();
            ////释放流所占用的资源
            //sfRead.Dispose();
            //Console.WriteLine(s);
            //Console.ReadKey();

            //使用Filestream写入数据
            //using (FileStream fsWrite = new FileStream(@"C:\Users\Administrator\Desktop\1.txt", System.IO.FileMode.OpenOrCreate, FileAccess.Write))
            //{
            //    string str = "看我有木有把你覆盖掉";
            //    byte[] buffer = Encoding.UTF8.GetBytes(str);
            //    fsWrite.Write(buffer, 0, buffer.Length);
            //}
            //Console.WriteLine("写入成功");
            //Console.ReadKey();


            //StreamReader和StreamWriter
            //使用StreamReader来读取一个文本文件
            //using (StreamReader sr = new StreamReader(@"C:\Users\Administrator\Desktop\1.txt"))
            //{
            //    while (!sr.EndOfStream)
            //    {
            //        Console.WriteLine(sr.ReadLine());
            //    }
            //}
            //Console.ReadKey();


            //使用StreamWiter来写入一个文件
            //如果不加入true则会覆盖掉之前的文字，加入true则不会覆盖
            using (StreamWriter sw = new StreamWriter(@"C:\Users\Administrator\Desktop\1.txt",true))
            {
                sw.Write("今天天气针不戳");
            }
            Console.WriteLine("写入成功");
            Console.ReadKey();
        }
    }
}
